﻿#include "StatisticsWorker.h"
#include <QDebug>
StatisticsWorker::StatisticsWorker(QString databaseType, QString databaseName, QObject *parent) : QObject(parent)
  ,m_dateFrom(QDate::currentDate().addDays(-7))
  ,m_dateTo(QDate::currentDate())
  ,m_tableName("statistics")
  ,m_showAll(false)
  ,m_isAllType(false)
  ,m_databaseType(databaseType)
  ,m_databaseName(databaseName)
{

}

StatisticsWorker::~StatisticsWorker()
{
    if(database!=nullptr){
        database->deleteLater();
        database = nullptr;
    }
}

void StatisticsWorker::onInit()
{
    database = new SqliteDatabase(NULL,m_databaseName,m_tableName, m_databaseType);
    initData();
}

void StatisticsWorker::flashStatistics()
{
    QDate date=m_dateFrom;

    QJsonObject staData;
    while (date<=m_dateTo) {
        auto record = database->find(m_tableName,date.toString("yyyy-MM-dd"));
        date = date.addDays(1);
        if(record.isEmpty())
            continue;
        QJsonObject jsonData =  record.at(0).toObject();
        QJsonObject data = jsonData["data"].toObject();

        for(int i=0;i<data.keys().size();++i){
            auto name = data.keys().at(i);
            auto number = data[name].toInt();

            if(!mapEngChn.isEmpty())
                name = mapEngChn[name];

            staData[name] = staData[name].toInt()+number;
        }
    }

    emit sigStatisticsUpdated(staData);
}

void StatisticsWorker::clearTodayStatistics()
{
    auto record = database->find(m_tableName,QDate::currentDate().toString("yyyy-MM-dd"));
    if(record.isEmpty())
        return;
    QJsonObject jsonData =  record.at(0).toObject();
    database->deleteRow(m_tableName,jsonData["id"].toString());
    flashStatistics();
}

void StatisticsWorker::onDateFromChanged(const QDate &from)
{
    m_dateFrom = from;
    flashStatistics();
}

void StatisticsWorker::onDateToChanged(const QDate &to)
{
    m_dateTo = to;
    flashStatistics();
}

void StatisticsWorker::clearDateStatistics(QDate date)
{
    auto record = database->find(m_tableName, date.toString("yyyy-MM-dd"));
    if(record.isEmpty())
        return;
    QJsonObject jsonData =  record.at(0).toObject();
    database->deleteRow(m_tableName,jsonData["id"].toString());
    flashStatistics();
}

void StatisticsWorker::onDateFromChangedByID(const QDate& from, const QString& findName)
{
    m_dateFrom = from;
    QDate date=m_dateFrom;

    QJsonObject staData;
    while (date<=m_dateTo) {
        auto record = database->find(m_tableName,date.toString("yyyy-MM-dd"));
        auto tmpDate = date;
        date = date.addDays(1);
        if(record.isEmpty())
        {
            staData[tmpDate.toString("yyyy-MM-dd")] = 0;
            continue;
        }
        QJsonObject jsonData =  record.at(0).toObject();
        QJsonObject data = jsonData["data"].toObject();

        for(int i=0;i<data.keys().size();++i){
            auto name = data.keys().at(i);
            auto number = data[name].toInt();
            if(name != findName)
            {
                staData[tmpDate.toString("yyyy-MM-dd")] = 0;
                continue;
            }

            if(!mapEngChn.isEmpty())
                name = mapEngChn[name];

            staData[tmpDate.toString("yyyy-MM-dd")] = number;
            break;
        }
    }

    emit sigStatisticsUpdated(staData);
}

void StatisticsWorker::onDateChanged(const QDate& from, const QDate& to)
{
    m_dateFrom = from;
    m_dateTo = to;

    QDate date=m_dateFrom;
    QJsonObject staData;
    while (date<=m_dateTo) {
        auto record = database->find(m_tableName,date.toString("yyyy-MM-dd"));
        date = date.addDays(1);
        if(record.isEmpty())
        {
            continue;
        }
        QJsonObject jsonData =  record.at(0).toObject();
        QJsonObject data = jsonData["data"].toObject();

        for(int i=0;i<data.keys().size();++i){
            auto name = data.keys().at(i);
            auto number = data[name].toInt();
            auto totalNum = staData[name].toInt()+number;
            staData[name] = totalNum;
        }
    }

    emit sigStatisticsUpdated(staData);
}

QJsonObject StatisticsWorker::getData()
{
    auto record = database->find(m_tableName,QDate::currentDate().toString("yyyy-MM-dd"));
    if(record.isEmpty()){
        StatisticsEntity entity;
        database->createRow(m_tableName,entity.mId,QJsonDocument::fromVariant(entity.toVariant()).object());
    }

    record = database->find(m_tableName,QDate::currentDate().toString("yyyy-MM-dd"));
    QJsonObject jsonData =  record.at(0).toObject();

    return jsonData["data"].toObject();
}

QVariant  StatisticsWorker::getData(QString key, QString date)
{
    auto record = database->find(m_tableName,date);
    if(record.isEmpty()){
        StatisticsEntity entity;
        database->createRow(m_tableName,entity.mId,QJsonDocument::fromVariant(entity.toVariant()).object());
    }

    record = database->find(m_tableName,date);
    QJsonObject jsonData =  record.at(0).toObject();

    return jsonData[key].toVariant();
}

void StatisticsWorker::setData(const QJsonObject &data)
{
    auto record = database->find(m_tableName,QDate::currentDate().toString("yyyy-MM-dd"));
    QJsonObject jsonData =  record.at(0).toObject();

    jsonData.insert("data",data);

    database->updateRow(m_tableName,jsonData["id"].toString(),jsonData);
    flashStatistics();
}

void StatisticsWorker::setData(QVariant data, QString key, QString date)
{
    auto record = database->find(m_tableName,date);
    QJsonObject jsonData =  record.at(0).toObject();
    jsonData[key] = data.toJsonValue();

    database->updateRow(m_tableName,jsonData["id"].toString(),jsonData);
}
